package com.api.shopapi.mapper;


import com.api.shopapi.entity.OrderBusinessAppraise;
import com.api.shopapi.entity.dto.OrderBusinessAppraiseDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface OrderBusinessAppraiseMapper {



    @Insert("insert into order_business_appraise value" +
            "(" +
            "#{orderBusinessAppraise.OrderBusinessAppraiseCode}," +
            "#{orderBusinessAppraise.OrderBusinessCode}," +
            "#{orderBusinessAppraise.Remarks}," +
            "#{orderBusinessAppraise.CreateTime}" +
            ")")
    public int insert(@Param("orderBusinessAppraise") OrderBusinessAppraise orderBusinessAppraise);

    @Delete("delete from order_business_appraise where OrderBusinessAppraiseCode=#{orderBusinessAppraiseCode}")
    public int delete(@Param("orderBusinessAppraiseCode") String orderBusinessAppraiseCode);

    @Update("update order_business_appraise set OrderBusinessCode = #{orderBusinessAppraise.OrderBusinessCode},Remarks = #{orderBusinessAppraise.Remarks} " +
            "where OrderBusinessAppraiseCode=#{orderBusinessAppraise.OrderBusinessAppraiseCode}")
    public int update(@Param("orderBusinessAppraise") OrderBusinessAppraise orderBusinessAppraise);

    @Select("select * from order_business_appraise order by CreateTime desc")
    public List<OrderBusinessAppraise> getList();

    @Select("select * from order_business_appraise where OrderBusinessAppraiseCode=#{orderBusinessAppraiseCode} limit 1")
    public OrderBusinessAppraise getByOrderBusinessAppraiseCode(@Param("orderBusinessAppraiseCode") String orderBusinessAppraiseCode);

    @Select("select * from order_business_appraise where orderBusinessCode=#{orderBusinessCode} limit 1")
    public OrderBusinessAppraise getByOrderBusinessCode(@Param("orderBusinessCode") String orderBusinessCode);


    //商家代表下的所有订单的评价
    @Select("select oba.* from " +
            "order_business ob join `order` o " +
            "on ob.OrderCode = o.OrderCode " +
            "join order_business_appraise oba "+
            "on oba.OrderBusinessCode = ob.OrderBusinessCode " +
            "where " +
            "and o.BusinessTopCode=#{businessTopCode} " +
            "<if test =' businessCode != \"00000000-0000-0000-0000-000000000000\" '> " +
            "and ob.businessCode = #{businessCode} " +
            "</if>" +
            "order by ob.CreateTime desc")
    List<OrderBusinessAppraiseDTO> getListByBusinessTopCodeBusinessCode(@Param("businessTopCode") String businessTopCode,
                                                                        @Param("businessCode") String businessCode);


    //政府下的所有订单的评价
    @Select("select oba.* from " +
            "order_business ob join `order` o " +
            "on ob.OrderCode = o.OrderCode " +
            "join order_business_appraise oba "+
            "on oba.OrderBusinessCode = ob.OrderBusinessCode " +
            "where " +
            "and o.governmentCode=#{governmentCode} " +
            "<if test =' businessCode != \"00000000-0000-0000-0000-000000000000\" '> " +
            "and ob.businessCode = #{businessCode} " +
            "</if>" +
            "order by ob.CreateTime desc")
    List<OrderBusinessAppraiseDTO> getListByGovernmentCodeBusinessCode(@Param("governmentCode") String governmentCode,
                                                                        @Param("businessCode") String businessCode);


    //所有评价（居委会，街道）
    @Select("select oba.* from " +
            "order_business ob join `order` o " +
            " on ob.OrderCode = o.OrderCode " +
            "join elderly e " +
            " on o.UserCode = e.UserCode " +
            "join order_business_appraise oba "+
            "on oba.OrderBusinessCode = ob.OrderBusinessCode " +
            " where " +
            "instr(e.ZoneCodePath,#{zoneCode}) order by ob.CreateTime desc")
    List<OrderBusinessAppraiseDTO> getListByZoneCode(@Param("zoneCode") String zoneCode);





    //查询商家评价列表
    @Select("select * from order_business_appraise as oba left join order_business as ob on oba.OrderBusinessCode=ob.OrderBusinessCode " +
            "left join business as b on ob.BusinessCode=b.BusinessCode where b.BusinessCode=#{businessCode} " +
            "order by oba.CreateTime desc")
    List<OrderBusinessAppraiseDTO> getBusinessPageList(@Param("businessCode") String businessCode);

    //查询商家评价列表
    @Select("select oba.*,e.ZoneCode from order_business_appraise as oba left join order_business as ob on oba.OrderBusinessCode=ob.OrderBusinessCode" +
            "left join business as b on ob.BusinessCode=b.BusinessCode " +
            "left join  `order` as o on o.OrderCode =ob.OrderCode " +
            "left join  elderly as e on e.UserCode=o.UserCode  " +
            "where b.BusinessCode=#{businessCode} and  e.ZoneCodePath like concat('%',#{zoneCode},'%') ")
    List<OrderBusinessAppraiseDTO> getBusinessPageListServe(@Param("businessCode") String businessCode,@Param("zoneCode") String zoneCode);


    @Select("select * from order_business_appraise oba  left join order_serve os on oba.OrderBusinessCode =os.OrderBusinessCode " +
            "where os.UserCode = #{userCode} " +
            "order by oba.CreateTime desc")
    List<OrderBusinessAppraiseDTO> getAppraisePageByServer(@Param("userCode") String userCode);

    //查询政府评价列表
    @Select("select * from order_business_appraise as oba left join order_business as ob on oba.OrderBusinessCode=ob.OrderBusinessCode " +
            "left join business as b on ob.BusinessCode=b.BusinessCode " +
            "left join government as g on b.GovernmentCode=g.GovernmentCode " +
            "where g.GovernmentCode=#{government} ")
    List<OrderBusinessAppraiseDTO> getGovernmentPageList(@Param("government") String government);

    //查询居委会 街道 评价列表
    @Select("<script>" +
            "select * from `order_business_appraise` as oba " +
            "left join `order_business`  as  ob on oba.OrderBusinessCode=ob.OrderBusinessCode " +
            "left join `order` as o on  ob.OrderCode=o.OrderCode " +
            "left join `user` as u on o.UserCode=u.UserCode " +
            "left join `elderly` as e on u.UserCode=e.UserCode " +
            "left join `zone` as z on z.ZoneCode =e.ZoneCode " +
            "<if test =' zoneCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
            " where e.ZoneCodePath like concat('%',#{zoneCode},'%')" +
            "</if>" +
            "</script>")
    List<OrderBusinessAppraiseDTO> getJWHAndJDPageList(@Param("zoneCode") String zoneCode);

    @Select("<script>" +
            "select * from order_business_appraise as oba left join order_business as ob on oba.OrderBusinessCode=ob.OrderBusinessCode " +
            "left join business as b on ob.BusinessCode=b.BusinessCode " +
            "left join government as g on b.GovernmentCode=g.GovernmentCode " +
            "where g.GovernmentCode=#{governmentCode} " +
            "<if test =' businessCode !=\"\" and businessCode!=null '>" +
            " and b.BusinessCode =#{businessCode} " +
            "</if>" +
            "</script>")
    List<OrderBusinessAppraiseDTO> getPageToGovPlatform(@Param("governmentCode") String governmentCode,
                                                        @Param("businessCode") String businessCode);
}
